<?php
/**
 * Page Template
 *
 * Loaded automatically by index.php?main_page=product_info.<br />
 * Displays details of a typical product
 *
 * @package templateSystem
 * @copyright Copyright 2003-2006 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: tpl_product_info_display.php 16242 2010-05-08 16:05:40Z ajeh $
 */
 //require(DIR_WS_MODULES . '/debug_blocks/product_info_prices.php');
//array of index that want to replace, they are 3, 5, 7
$array_indexes = array(3, 5, 7);
// FUNCTION TO CONVERT TRAPS - FOLLOW PRODUCT NAME + MANUFACTURER + CATEGORY
// $products_description
// $products_name, <a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $_GET['cPath'] . '&products_id=' . $_GET['products_id']) . '">' . $products_name . '</a>
// $manufacturers_name, '<a href="' . zen_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $manufacturers_id) . '">' . $manufacturers_name . '</a>'
// $categories->fields['categories_name'], '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $_GET['cPath']) . '">' . $categories_name . '</a>'
function replace_strap($find, $replace, $string, $arr_index)
{
    $str_return = "";
    $str_temp = "";
    $pattern = "|$find|i";
    $index = 1;
    preg_match($pattern , $string, $match, PREG_OFFSET_CAPTURE);
    while(sizeof($match) > 0)
    {        
        $pos_find = $match[0][1] + strlen($find);
        
        $str_temp = substr($string, 0, $pos_find);
        
        if(in_array($index, $arr_index) && strpos($str_temp, '<img ') === false && strpos($str_temp, '/') === false)
        {            
            $str_temp = preg_replace($pattern, $replace, $str_temp, 1);
        }
        $str_return .= $str_temp;
        
        $string = substr($string, $pos_find, strlen($string));
        preg_match($pattern, $string, $match, PREG_OFFSET_CAPTURE);
        $index++;
    }
    $str_return .= $string;
    
    return $str_return;
}
$input = "plain [products_id=12] plain [products_id=13]";
function parseTagsRecursive($input)
{

    $pattern = "/\[products_id=([0-9]*)\]/i";
    //preg_replace_callback($pattern, 'parseTagsRecursive', $input);
    //preg_match($pattern , $input, $match, PREG_OFFSET_CAPTURE);
    if (is_array($input)) {
        var_dump($input);exit;
        $input = '<div style="margin-left: 10px">'.$input[1].'</div>';
        //echo $input;exit;
    }

    return preg_replace_callback($pattern, 'parseTagsRecursive', $input);
}

//$output = parseTagsRecursive($input);

//echo $output; exit;

function replace_strap_new($find, $replace, $string)
{
    $str_return = "";
    $str_temp = "";
    $pattern = "|$find|i";    
    preg_match($pattern , $string, $match, PREG_OFFSET_CAPTURE);
    while(sizeof($match) > 0)
    {        
        $pos_find = $match[0][1] + strlen($find);
        
        $str_temp = substr($string, 0, $pos_find);        
        
        $str_temp = preg_replace($pattern, $replace, $str_temp, 1);
 
        $str_return .= $str_temp;
        
        $string = substr($string, $pos_find, strlen($string));
        preg_match($pattern, $string, $match, PREG_OFFSET_CAPTURE);
        $index++;
    }
    $str_return .= $string;
    
    return $str_return;
}

function straps($p_description, $p_name, $p_id, $arr_indexes)
{
    global $db;
    //replace name    
    $find = false;
    $pattern = "|$p_name|i";
    preg_match($pattern , $p_description, $match, PREG_OFFSET_CAPTURE);
    if(!sizeof($match))
    {
        while(!sizeof($match))
        {
            $pos = strrpos($p_name, " ");
            if($pos === false)
                break;
            $p_name = substr($p_name, 0, $pos);      
            $pattern = "/$p_name/i";
            preg_match($pattern , $p_description, $match, PREG_OFFSET_CAPTURE);
            if(sizeof($match))
            {                
                $find = true;
                break;
            }            
        }
    }
    else {
        $find = true;
    }    
    if($find)
    {        
        $link_product = '<a href="' . zen_href_link(zen_get_info_page($p_id), 'cPath=' . $_GET['cPath'] . '&products_id=' . $p_id) . '">' . $match[0][0] . '</a>';
        $p_description = replace_strap(str_replace("/", "\/", $match[0][0]), "URBANO_PRODUCT_LINK_STRAPS", $p_description, $arr_indexes);
    }
    //replace manufacturer
    $manufacturer_query = $db -> Execute("select m.manufacturers_id, m.manufacturers_name
  					from " . TABLE_MANUFACTURERS . " m join 
                                        " . TABLE_MANUFACTURERS_INFO . " mi on m.manufacturers_id = mi.manufacturers_id join
                                        " . TABLE_PRODUCTS . " p on p.manufacturers_id = m.manufacturers_id
					where p.products_id = " . (int)$p_id . " and mi.languages_id = '" . (int)$_SESSION['languages_id'] . "'");
    
    if (!$manufacturer_query -> EOF) {
        $m_name = $manufacturer_query->fields["manufacturers_name"];
        $m_id = $manufacturer_query->fields["manufacturers_id"];
    }    
    if(isset($m_id) && isset($m_name))
    {
        $link_manufacturer = '<a href="' . zen_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $m_id) . '">' . $m_name . '</a>';
        $p_description = replace_strap(str_replace("/", "\/", $m_name), "URBANO_MANUFACTURER_LINK_STRAPS", $p_description, $arr_indexes);
    }
    
    //replace category
    $category_query = $db -> Execute("select cd.categories_id, cd.categories_name
  					from " . TABLE_CATEGORIES_DESCRIPTION . " cd join
                        " . TABLE_PRODUCTS_TO_CATEGORIES . " p on cd.categories_id = p.categories_id
					where p.products_id = " . (int)$p_id . " and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'");
    
    if (!$category_query -> EOF) {
        $c_name = $category_query->fields["categories_name"];
        $c_id = $category_query->fields["categories_id"];
    }
    if(isset($c_name) && isset($c_id))
    {
        $link_category = '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $_GET['cPath']) . '">' . $c_name . '</a>';
        $p_description = replace_strap(str_replace("/", "\/", $c_name), "URBANO_CATEGORY_LINK_STRAPS", $p_description, $arr_indexes);
    }
    
    if(isset($link_product))
        $p_description = preg_replace('/URBANO_PRODUCT_LINK_STRAPS/', $link_product, $p_description);
    
    if(isset($link_manufacturer))
        $p_description = preg_replace('/URBANO_MANUFACTURER_LINK_STRAPS/', $link_manufacturer, $p_description);
    
    if(isset($link_category))
        $p_description = preg_replace('/URBANO_CATEGORY_LINK_STRAPS/', $link_category, $p_description);
    
    return $p_description;
}
// END OF STRAPS FUNCTIONS
?>
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<div class="centerColumn" id="productGeneral">

<div id="productGeneralleft">
<?php if ($messageStack->size('product_info') > 0) echo $messageStack->output('product_info'); ?>

<!--bof Category Icon -->
<?php if ($module_show_categories != 0) {?>
<?php
/**
 * display the category icons
 */
require($template->get_template_dir('/tpl_modules_category_icon_display.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_category_icon_display.php'); ?>
<?php } ?>
<!--eof Category Icon -->

<!--bof Prev/Next top position -->

<!--eof Prev/Next top position-->

<!--bof Main Product Image -->
<?php
  if (zen_not_null($products_image)) {
  ?>
<?php
/**
 * display the main product image
 */
   require($template->get_template_dir('/tpl_modules_main_product_image.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_main_product_image.php'); ?>
<?php
  }
?>
<!--eof Main Product Image-->

<!--bof Product Name-->

<h1 id="productName" class="productGeneral"><?php echo $products_short_name; ?></h1>
<h2><?php   echo (($flag_show_product_info_manufacturer == 1 and !empty($manufacturers_name)) ? '<a href="' . zen_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $manufacturers_id) . '">' . $manufacturers_name . '</a>' : '') . "\n";   ?></h2>
<?php if (!empty($products_tagline)) { 
				echo '<p class="biggerText">'. $products_tagline . '</p>'; 
				} ?>
<!--eof Product Name-->

<p> </p>
<?php
  if ($flag_show_product_info_reviews == 1) {
    // if more than 0 reviews, then show reviews button; otherwise, show the "write review" button
    if ($reviews->fields['count'] > 0 ) { ?>
<?php
         if ($flag_show_product_info_reviews_count == 1) {
           $stars_image_suffix = str_replace('.', '_', zen_round($reviews_average_rating->fields['average_rating'] * 2, 0) / 2); // for stars_0_5.gif, stars_1.gif, stars_1_5.gif etc.
           $average_rating = zen_round($reviews_average_rating->fields['average_rating'], 2);
           echo '<p>'. zen_image(DIR_WS_TEMPLATE_IMAGES . 'stars_' . $stars_image_suffix . '.gif', sprintf(BOX_REVIEWS_TEXT_OF_5_STARS, $average_rating));
           if((int)$reviews->fields['count'] === 1)
               echo '<span style="vertical-align: 7px"><a href="' . zen_href_link(FILENAME_PRODUCT_REVIEWS, zen_get_all_get_params()) . '">' . TEXT_PRODUCTS_INFO_ONE_REVIEW . '</a></span>';
           else
               echo '<span style="vertical-align: 7px"><a href="' . zen_href_link(FILENAME_PRODUCT_REVIEWS, zen_get_all_get_params()) . '">' . sprintf(TEXT_PRODUCTS_INFO_MULTIPLE_REVIEWS, $reviews->fields['count']) . '</a></span>';
           echo '</p>';           
         } else {
           echo '';
         }
       ?><?php } 
    else { 
        echo '<a href="' . zen_href_link(FILENAME_PRODUCT_REVIEWS_WRITE, zen_get_all_get_params()) . '">' . TEXT_PRODUCTS_INFO_NO_REVIEW . '</a>';
    }
  }
?>


<!--bof Product details list  -->
<?php if ( (($flag_show_product_info_model == 1 and $products_model != '') or ($flag_show_product_info_weight == 1 and $products_weight !=0) or ($flag_show_product_info_quantity == 1) or ($flag_show_product_info_manufacturer == 1 and !empty($manufacturers_name))) ) { ?>
<ul id="productDetailsList" class="floatingBox back">
  <?php echo (($flag_show_product_info_model == 1 and $products_model !='') ? '<li>' . TEXT_PRODUCT_MODEL . $products_model . '</li>' : '') . "\n"; ?>
  <?php echo (($flag_show_product_info_weight == 1 and $products_weight !=0) ? '<li>' . TEXT_PRODUCT_WEIGHT .  $products_weight . TEXT_PRODUCT_WEIGHT_UNIT . '</li>'  : '') . "\n"; ?>
  <?php echo (($flag_show_product_info_quantity == 1) ? '<li>' . $products_quantity . TEXT_PRODUCT_QUANTITY . '</li>'  : '') . "\n"; ?>
</ul>
<?php
  }
?>
<!--eof Product details list -->
<br class="clearBoth" />
<!--bof Reviews button and count-->

<a href="http://twitter.com/share" class="twitter-share-button" data-count="none" data-via="suplementos_on">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<g:plusone></g:plusone>
<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(zen_href_link(zen_get_info_page((int)$_GET['products_id']),'cPath='.(int)$_GET['cPath'].'&products_id=' . (int)$_GET['products_id'])); ?>&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:370px; height:24px; margin-left: -40px"></iframe>
<!--bof Tell a Friend button -->
<?php
  if ($flag_show_product_info_tell_a_friend == 1) { ?>
<?php echo ($flag_show_product_info_tell_a_friend == 1 ? '<a href="' . zen_href_link(FILENAME_TELL_A_FRIEND, 'products_id=' . $_GET['products_id']) . '">' . zen_image_button(BUTTON_IMAGE_TELLAFRIEND, BUTTON_TELLAFRIEND_ALT) . '</a>' : ''); ?>
<?php
  }
?>
<!--eof Tell a Friend button -->&nbsp;
<!--bof free ship icon  -->
<?php if(zen_get_product_is_always_free_shipping($products_id_current) && $flag_show_product_info_free_shipping) { ?>
<div id="freeShippingIcon"><?php echo TEXT_PRODUCT_FREE_SHIPPING_ICON; ?></div>
<?php } ?>
<!--eof free ship icon  -->
<!--bof show products alternative -->
<?php
    if($products_quantity <= 0)
    {
        $listing_sql_altenative = "
                    select distinct p.products_image, pd.products_name, p.products_quantity, p.products_id, p.products_type, p.master_categories_id
                        , p.manufacturers_id, m.manufacturers_name, p.products_price, p.products_tax_class_id, pd.products_description
                        , IF(s.status = 1, s.specials_new_products_price, NULL) as specials_new_products_price
                        , IF(s.status =1, s.specials_new_products_price, p.products_price) as final_price, p.products_sort_order, p.product_is_call
                        , p.product_is_always_free_shipping, p.products_qty_box_status 
                    from " . TABLE_PRODUCTS_DESCRIPTION . " pd, " .
                        TABLE_PRODUCTS . " p left join " . TABLE_MANUFACTURERS . " m on p.manufacturers_id = m.manufacturers_id, " .
                        TABLE_PRODUCTS_TO_CATEGORIES . " p2c left join " . TABLE_SPECIALS . " s on p2c.products_id = s.products_id 
                    where p.products_status = 1 and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '" . (int)$_SESSION['languages_id'] . "' 
                        and p2c.categories_id in (select distinct p2c1.categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES ." p2c1 where p2c1.products_id = " . (int)$_GET['products_id'] . ")  
                        and p.products_quantity > 0 and p.products_ordered > 5
                    order by abs(p.products_price - $products_price) asc, p.products_sort_order asc, (case when p.products_quantity >= 1 then 1 else 0 end) desc, p.products_ordered desc
                    limit 0,3";
        //echo($listing_sql_altenative);
        $listing = $db->Execute($listing_sql_altenative);
        if($listing->RecordCount() > 0)
        {
            
            echo '<div id="productListing">';
            echo '<h2>' . TEXT_PRODUCTS_LISTING_SHOW_ALTERNATIVE . '</h2><br />';            
            while(!$listing->EOF && $i < 3)
            {                
                $manu_link = "";
                $review_status = " and r.status = '1'";

                $reviews_query = "select count(*) as count from " . TABLE_REVIEWS . " r, "
                                                                . TABLE_REVIEWS_DESCRIPTION . " rd
                                where r.products_id = '" . (int)$listing->fields['products_id'] . "'
                                and r.reviews_id = rd.reviews_id
                                and rd.languages_id = '" . (int)$_SESSION['languages_id'] . "'" .
                                $review_status;

                $reviews = $db->Execute($reviews_query);
                $reviews_average_rating_query = "select avg(reviews_rating) as average_rating from " . TABLE_REVIEWS . " r, "
                                                                . TABLE_REVIEWS_DESCRIPTION . " rd
                                where r.products_id = '" . (int)$listing->fields['products_id'] . "'
                                and r.reviews_id = rd.reviews_id
                                and rd.languages_id = '" . (int)$_SESSION['languages_id'] . "'" .
                                $review_status;

                $reviews_average_rating = $db->Execute($reviews_average_rating_query);
                $flag_show_product_info_reviews_alternate = zen_get_show_product_switch($listing->fields['products_id'], 'reviews');
                $flag_show_product_info_reviews_count_alternative = zen_get_show_product_switch($listing->fields['products_id'], 'reviews_count');
                $review_alternate = '';
                if ($flag_show_product_info_reviews_alternate == 1) {
                    // if more than 0 reviews, then show reviews button; otherwise, show the "write review" button
                    if ($reviews->fields['count'] > 0 ) 
                        if ($flag_show_product_info_reviews_count_alternative == 1) {
                        $stars_image_suffix = str_replace('.', '_', zen_round($reviews_average_rating->fields['average_rating'] * 2, 0) / 2); // for stars_0_5.gif, stars_1.gif, stars_1_5.gif etc.
                        $average_rating = zen_round($reviews_average_rating->fields['average_rating'], 2);
                        $review_alternate = zen_image(DIR_WS_TEMPLATE_IMAGES . 'stars_' . $stars_image_suffix . '.gif', sprintf(BOX_REVIEWS_TEXT_OF_5_STARS, $average_rating));
                    } 
                } 
                
                
                if($listing->fields['manufacturers_id'] > 0)
                    $manu_link = '<a href="' . zen_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $listing->fields['manufacturers_id']) . '">' . $listing->fields['manufacturers_name'] . '</a><br />';
                echo '<div class="centerBoxContentsProducts centeredContent back" style="width:32.5%;">';
                echo '<a href="' . zen_href_link(zen_get_info_page($listing->fields['products_id']), 'cPath=' . (($_GET['manufacturers_id'] > 0 and $_GET['filter_id']) > 0 ?  zen_get_generated_category_path_rev($_GET['filter_id']) : ($_GET['cPath'] > 0 ? zen_get_generated_category_path_rev($_GET['cPath']) : zen_get_generated_category_path_rev($listing->fields['master_categories_id']))) . '&products_id=' . $listing->fields['products_id']) . '">' . zen_image(DIR_WS_IMAGES . $listing->fields['products_image'], $listing->fields['products_name'], IMAGE_PRODUCT_LISTING_WIDTH, IMAGE_PRODUCT_LISTING_HEIGHT, 'class="listingProductImage"') . '</a><br />';
                echo '<h3>' . $manu_link . '<a href="' . zen_href_link(zen_get_info_page($listing->fields['products_id']), 'cPath=' . (($_GET['manufacturers_id'] > 0 and $_GET['filter_id']) > 0 ?  zen_get_generated_category_path_rev($_GET['filter_id']) : ($_GET['cPath'] > 0 ? zen_get_generated_category_path_rev($_GET['cPath']) : zen_get_generated_category_path_rev($listing->fields['master_categories_id']))) . '&products_id=' . $listing->fields['products_id']) . '">' . $listing->fields['products_name'] . '</a></h3>';
               
                echo zen_get_products_display_price($listing->fields['products_id']) . '<br />' . $review_alternate;
                echo '</div>';
                $listing->MoveNext();
            }            
            echo '</div>';
            echo '<br class="clearBoth" />';
        }
    }
?>
<!--eof show products alternative -->
<br class="clearBoth" />
 <!--bof Expirations Date -->
 <?php
    if(isset($products_expiration_date))
    {
        //echo(time());exit;
        $days = floor((strtotime($products_expiration_date) - time()) / (60 * 60 * 24));        
        if($days < 90 && $days > 0)
        {   
            echo sprintf(TEXT_PRODUCTS_EXPIRATIONS, strtoupper(strftime('%B', strtotime($products_expiration_date))), strftime('%Y', strtotime($products_expiration_date)));
        }
    }
 ?>
 <!--eof Expirations Date -->

 <!--bof Product description -->
<?php if ($products_description != '') { ?>
<div id="productDescription" class="productGeneral biggerText">

    <?php echo straps(stripslashes($products_description),$products_name, $_GET['products_id'], $array_indexes) ; ?></div>
<?php } ?>
<!--eof Product description -->
<br class="clearBoth" />
<?php
  if ($reviews_split->number_of_rows > 0) {
    if ((PREV_NEXT_BAR_LOCATION == '1') || (PREV_NEXT_BAR_LOCATION == '3') && 1==0) {//never use paging
?>

<div id="productReviewsDefaultListingTopNumber" class="navSplitPagesResult"><?php echo $reviews_split->display_count(TEXT_DISPLAY_NUMBER_OF_REVIEWS); ?></div>

<div id="productReviewsDefaultListingTopLinks" class="navSplitPagesLinks"><?php echo TEXT_RESULT_PAGE . ' ' . $reviews_split->display_links(MAX_DISPLAY_PAGE_LINKS, zen_get_all_get_params(array('page', 'info', 'main_page'))); ?></div>

<?php
    }
    //Vu - remove and change if state
    if(sizeof($reviewsArray) > 0){  
        $list = listReviews($db, '', $reviewsArray);
        echo '<div id="productReviews">' . $list . '</div>';
    }
    //Vu - end edit
?>
<?php
  } else {
?>
<?php
  }

  if (($reviews_split->number_of_rows > 0) && ((PREV_NEXT_BAR_LOCATION == '2') || (PREV_NEXT_BAR_LOCATION == '3')) && 1==0) {//never use paging
?>
<hr />
<div id="productReviewsDefaultListingBottomNumber" class="navSplitPagesResult"><?php echo $reviews_split->display_count(TEXT_DISPLAY_NUMBER_OF_REVIEWS); ?></div>
<div id="productReviewsDefaultListingBottomLinks" class="navSplitPagesLinks"><?php echo TEXT_RESULT_PAGE . ' ' . $reviews_split->display_links(MAX_DISPLAY_PAGE_LINKS, zen_get_all_get_params(array('page', 'info', 'main_page'))); ?></div>

<?php
  }  
?>
    <?php 
        if ($reviews_split->number_of_rows > 3)
            echo '<div id="productReviewsDefaultListingBottomNumber" class="navSplitPagesResult">' . sprintf(REVIEW_HOWMANY, 3, $reviews_split->number_of_rows) . '</div><br />';
    ?>
    <div class="centeredContent">
    <?php if($reviews_split->number_of_rows > 3) {?>
    <?php echo ' <a href="' . zen_href_link(FILENAME_PRODUCT_REVIEWS, zen_get_all_get_params(array('reviews_id'))) . '">' . zen_image_button(BUTTON_IMAGE_REVIEWS, BUTTON_REVIEWS_ALT) . '</a>'; ?>
    <?php }?>
    <?php echo '<a href="' . zen_href_link(FILENAME_PRODUCT_REVIEWS_WRITE, zen_get_all_get_params(array('reviews_id'))) . '">' . zen_image_button(BUTTON_IMAGE_WRITE_REVIEW, BUTTON_WRITE_REVIEW_ALT) . '</a>'; ?>
    </div>
<br class="clearBoth" />
<!--bof Quantity Discounts table -->
<?php
  if ($products_discount_type != 0) { ?>
<?php
/**
 * display the products quantity discount
 */
 require($template->get_template_dir('/tpl_modules_products_quantity_discounts.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_products_quantity_discounts.php'); ?>
<?php
  }
?>
<!--eof Quantity Discounts table -->

<!--bof Additional Product Images -->
<?php
/**
 * display the products additional images
 */
  require($template->get_template_dir('/tpl_modules_additional_images.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_additional_images.php'); ?>
<!--eof Additional Product Images -->

<!--bof Prev/Next bottom position -->
<?php if (PRODUCT_INFO_PREVIOUS_NEXT == 2 or PRODUCT_INFO_PREVIOUS_NEXT == 3) { ?>
<?php
/**
 * display the product previous/next helper
 */
 require($template->get_template_dir('/tpl_products_next_previous.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_products_next_previous.php'); ?>
<?php } ?>
<!--eof Prev/Next bottom position -->


<!--bof Product date added/available-->
<?php
  if ($products_date_available > date('Y-m-d H:i:s')) {
    if ($flag_show_product_info_date_available == 1) {
?>
  <p id="productDateAvailable" class="productGeneral centeredContent"><?php echo sprintf(TEXT_DATE_AVAILABLE, zen_date_long($products_date_available)); ?></p>
<?php
    }
  } else {
    if ($flag_show_product_info_date_added == 1) {
?>
      <p id="productDateAdded" class="productGeneral centeredContent"><?php echo sprintf(TEXT_DATE_ADDED, zen_date_long($products_date_added)); ?></p>
<?php
    } // $flag_show_product_info_date_added
  }
?>
<!--eof Product date added/available -->

<!--bof Product URL -->
<?php
  if (zen_not_null($products_url)) {
    if ($flag_show_product_info_url == 1) {
?>
    <p id="productInfoLink" class="productGeneral centeredContent"><?php echo sprintf(TEXT_MORE_INFORMATION, zen_href_link(FILENAME_REDIRECT, 'action=url&goto=' . urlencode($products_url), 'NONSSL', true, false)); ?></p>
<?php
    } // $flag_show_product_info_url
  }
?>
<!--eof Product URL -->

<!--bof also purchased products module-->
<?php require($template->get_template_dir('tpl_modules_also_purchased_products.php', DIR_WS_TEMPLATE, $current_page_base,'templates'). '/' . 'tpl_modules_also_purchased_products.php');?>
<!--eof also purchased products module-->

</div>
<div id="productGeneralright">
    <?php require($template->get_template_dir('/tpl_products_grouping_display.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_products_grouping_display.php'); ?>
    <!-- bof Product dosis and label -->
    <?php if ($products_dosis != '' && sizeof($arr_products_label) > 0) { 
            $informula = "";
            $other_ingredients = "";
            $allergen_info = "";
            $amino_acid_profile = "";
            echo '<div><a href="javascript:void(0)" onclick="changeServing(true)">' . SERVING . '</a>&nbsp;&nbsp;&nbsp;<a href="javascript:void(0)" onclick="changeServing(false)">' . P100G . '</a></div>';
            echo '<div id="productLabel"><h3>' . PRODUCTS_COMPOSITION . ' ' . $products_short_name . '</h3><br />';        
            echo '<div id="LabelTable"><table><tr><td colspan="2">' . SERVING_SIZE . ': ' . $products_serving_measurement . '<td></tr><tr><td>' . NUMBER_DOSIS . ':</td><td id="products_dosis">' . $products_dosis . '</td></tr></table>';                    
            echo "<table border='0' cellspacing='0' cellpadding='1' style='width: 100%'>";
            echo '<tr><td style="width: 40%">&nbsp;</td><td align="right" style="width: 30%">' . PER_SERVING . '</td><td align="right" style="width: 30%">' . DAILY_VALUE . '</td></tr>';
            for($i = 0, $n = sizeof($arr_products_label); $i < $n; $i++)
            {
                $item = $arr_products_label[$i];
                if($item["type"] == 3)//informula
                {
                    $informula .= $item["description"] . ", ";
                }
                else if($item["type"] == 1)//table
                {
                    if($informula !== "")
                    {
                        echo "<tr><td style='border-bottom: 1px solid #000' colspan='3'>" . trim($informula, ", ") . "</td></tr>";
                        $informula = "";
                    }                    
                    if($item["ddd_serving"] == 0)
                    {
                        echo "<tr><td style='border-bottom: 1px solid #000'>" . $item["description"] . "</td><td style='border-bottom: 1px solid #000' align='right'><span class='serving'>" . $item["qty_serving"] . "</span>" 
                                . "<span class='100g' style='display:none'>" . $item["qty_100"] . "</span>" . " " . $item["unit"] . "</td><td style='border-bottom: 1px solid #000' align='right'>*</td></tr>";
                    }
                    else
                    {
                        echo "<tr><td style='border-bottom: 1px solid #000'>" . $item["description"] . "</td><td style='border-bottom: 1px solid #000' align='right'><span class='serving'>" . $item["qty_serving"] . "</span>" 
                                . "<span class='100g' style='display:none'>" . $item["qty_100"] . "</span>" . " " . $item["unit"] . "</td><td style='border-bottom: 1px solid #000' align='right'><span class='serving'>" 
                                . $item["ddd_serving"] . "%</span><span class='100g' style='display:none'>" . $item["ddd_100"] . "%</span></td></tr>";
                    }
                }
                else if($item["type"] == 7)//percentage
                {
                    if($informula !== "")
                    {
                        echo "<tr><td style='border-bottom: 1px solid #000' colspan='3'>" . trim($informula, ", ") . "</td></tr>";
                        $informula = "";
                    }                
                    echo "<tr><td style='border-bottom: 1px solid #000' colspan='2'>" . $item["description"] . "</td><td style='border-bottom: 1px solid #000' align='right'>" . $item["qty_serving"] . "%</td></tr>";                
                }
                else if($item["type"] == 2)//other ingredients
                {
                    $other_ingredients .= $item["description"] . ", ";
                }
                else if ($item["type"] == 6) //allergen info
                {
                    $allergen_info .= $item["description"] . ",";
                }
                else if($item["type"] == 4)//table_forward
                {
                    if($informula !== "")
                    {
                        echo "<tr><td style='border-bottom: 1px solid #000' colspan='3'>" . trim($informula, ", ") . "</td></tr>";
                        $informula = "";
                    }                    
                    if($item["ddd_serving"] == 0)
                    {
                        echo "<tr><td style='padding-left: 25px; border-bottom: 1px solid #000'>" . $item["description"] . "</td><td style='border-bottom: 1px solid #000' align='right'><span class='serving'>" . $item["qty_serving"] . "</span>" 
                                . "<span class='100g' style='display:none'>" . $item["qty_100"] . "</span>" . " " . $item["unit"] . "</td><td style='border-bottom: 1px solid #000' align='right'>*</td></tr>";
                    }
                    else
                    {
                        echo "<tr><td style='padding-left: 25px; border-bottom: 1px solid #000'>" . $item["description"] . "</td><td style='border-bottom: 1px solid #000' align='right'><span class='serving'>" . $item["qty_serving"] . "</span>" 
                                . "<span class='100g' style='display:none'>" . $item["qty_100"] . "</span>" . " " . $item["unit"] . "</td><td style='border-bottom: 1px solid #000' align='right'><span class='serving'>" 
                                . $item["ddd_serving"] . "%</span><span class='100g' style='display:none'>" . $item["ddd_100"] . "%</span></td></tr>";
                    }
                }
                else//amino_acid_profile
                {                
                    $amino_acid_profile .= "<tr><td style='border-bottom: 1px solid #000'>" . $item["description"] . "</td><td style='border-bottom: 1px solid #000' align='right'>" . $item["qty_serving"] . "</td></tr>";                
                }
            }
            if($informula !== "")
            {
                echo "<tr><td colspan='3' style='border-bottom: 1px solid #000'>" . trim($informula, ", ") . "</td></tr>";
                $informula = "";
            }
            if($other_ingredients !== "")
            {
                echo '<tr><td colspan="3" style="border-bottom: 1px solid #000">' . OTHER_INGREDIENTS . ': ' . trim($other_ingredients, ", ") . '</td></tr>';
                $other_ingredients = "";
            }
            if($allergen_info !== "")
            {
                echo '<tr><td colspan="3">' . ALLERGEN_INFORMATION . ': ' . trim($allergen_info, ", ") . '</td></tr>';
                $allergen_info = "";
            }
            echo "</table>";        
            echo '</div>';
            //draw table amino acid
            if($amino_acid_profile !== "")
            {
                echo '<div><table border="0" cellspacing="0" cellpadding="1" style="width: 100%"><tr><td colspan="2">' . TYPICAL_AMINO_ACID_PROFILE . '</td></tr>'
                        . $amino_acid_profile .
                    '</table></div>';
            }
            echo '</div>';
        }
     ?>
    <!-- eof Product dosis and label -->
    <!--bof Product usage -->
    <?php if ($products_directions != '') { 
        echo '<div id="productUsage"><h3>' . PRODUCTS_USAGE . ' ' . $products_short_name . '</h3>';
        echo '<p>' . $products_directions . '</p>' . '</div>' ;      
    } ?>
    <!--eof Product usage -->
	<!--bof Product usage -->
    <?php if ($products_warnings != '') { 
        echo '<div id="productWarning"><p><span style="font-weight: bold;">' . PRODUCTS_WARNING . ':</span> ' . $products_warnings . '</p></div>' ;      
    } ?>
    <!--eof Product usage -->
    <br />
    <div id="productDisclaimer"><?php echo '<p>' . sprintf(PRODUCT_DISCLAIMERS, $products_short_name) . '</p>'; ?></div></div>
</div>
<br class="clearBoth">
<!--bof Form close-->
	
<form method="POST" action="<?php echo zen_href_link(FILENAME_PRODUCT_INFO, zen_get_all_get_params(),'NONSSL') ?>" name="reviews_yes_no">
    <input type="hidden" value="" id="hd_review_yes_no" name="reviews_yes_no" />        
    <input type="hidden" value="" id="hd_review_yes_no_id" name="reviews_yes_no_id" />        
</form>
<?php
//Vu - add this function
function listReviews($db, $parentid, $reviewsArray, $res = '', $level = ''){
    foreach($reviewsArray as $reviews){
        if($reviews['reviewParentId'] == $parentid){
            $re = "<div class='child{$level}'><p>" ;
            if($parentid == NULL){
                $re .= $reviews["reviewsYes"] . " " . REVIEW_USEFUL . " " . $reviews["reviewsVotes"]. " " . REVIEW_USEFUL2 . "<br />" 
                    . zen_image(DIR_WS_TEMPLATE_IMAGES . 'stars_' . $reviews['reviewsRating'] . '.gif') ;
            
                $re .= "<span class='biggerText' style='vertical-align: 7px'><strong><a href='" . zen_href_link(FILENAME_PRODUCT_REVIEWS_INFO, 'products_id=' . (int)$_GET['products_id'] . '&reviews_id=' . $reviews['id']) . "'>" . $reviews["reviewsTitle"] . "</a></strong></span>"
                    . "<span style='vertical-align: 7px'> - <em>" . zen_date_short($reviews['dateAdded']) . "</em></span><br />"
                    . sprintf(TEXT_REVIEW_BY, $reviews['customersName'])."</p>";
            }
            $re .=   "<p class='biggerText' >" .  $reviews['reviewsText'] . "</p>";
            if($parentid == NULL){
                $re .= $reviews["owner"]; 
            }
            $re .= "<p>". REVIEW_USEFUL3;
            $reviews_votes_sql = "SELECT votes_id FROM votes WHERE reviews_id = :reviewsID AND customers_id = :customerID";
            $reviews_votes_sql = $db->bindVars($reviews_votes_sql, ':reviewsID', $reviews['id'], 'integer');
            $reviews_votes_sql = $db->bindVars($reviews_votes_sql, ':customerID', $_SESSION['customer_id'], 'integer');
            $reviews_votes_result = $db->Execute($reviews_votes_sql);

            if($reviews_votes_result->EOF){
                  $re .= ' <input type="button" onclick="reviews(1,' . $reviews["id"] . ')" value="' 
                          . REVIEW_YES_BUTTON . '" style="width: 30px" /> <input type="button" onclick="reviews(0,' . $reviews["id"] . ')" value="' 
                          . REVIEW_NO_BUTTON . '" style="width: 30px" /> '; 
            }
            else{

                $reviews_votes_countyes_sql = "SELECT count(votes_id) as countyes FROM votes WHERE reviews_id = :reviewsID AND yes_no_vote = 1";
                $reviews_votes_countyes_sql = $db->bindVars($reviews_votes_countyes_sql, ':reviewsID', $reviews['id'], 'integer');
                $reviews_votes_countyes_result = $db->Execute($reviews_votes_countyes_sql);

                $reviews_votes_countno_sql = "SELECT count(votes_id) as countno FROM votes WHERE reviews_id = :reviewsID AND yes_no_vote = 0";
                $reviews_votes_countno_sql = $db->bindVars($reviews_votes_countno_sql, ':reviewsID', $reviews['id'], 'integer');
                $reviews_votes_countno_result = $db->Execute($reviews_votes_countno_sql);

               $re .= " " . "VOTES: " . ($reviews_votes_countyes_result->fields['countyes'] - $reviews_votes_countno_result->fields['countno']). " " . ALREADY_VOTES_THIS_REVIEW;
            }

            $re .= REVIEW_USEFUL3;
            $re .= "<input type='button' id='{$reviews["id"]}' class='comment-button' value='Comment'/>";
            $re .= "</p></div>";
            $res .= listReviews($db, $reviews['id'], $reviewsArray, $re, $level.'1');
        }
    }
    return $res;
}
//Vu - end edit
?>
<script type="text/javascript">    
    function reviews(yesno, rid)
    {
        $('#hd_review_yes_no').val(yesno);
        $('#hd_review_yes_no_id').val(rid);
        $('[name=reviews_yes_no]').submit();
    }
    
    function selector()
    {
        var value = $('#size_selector').val();
        if($('#products_dosis').length)
            $('#products_dosis').html(value);
    }
    
    function changeServing(serving)
    {
        if(serving)
        {
            $('.serving').each(function(){$(this).show();});
            $('.100g').each(function(){$(this).hide();});
        }
        else
        {
            $('.serving').each(function(){$(this).hide();});
            $('.100g').each(function(){$(this).show();});
        }
    }
    
</script>